package CompleteFutureDemo;

import common.CalcTask;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;

/**
 * @author chao
 * @create 2022-02-05-20:28
 */
public class ThenAcceptTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ForkJoinPool joinPool = new ForkJoinPool();
        CompletableFuture<Double> cf = CompletableFuture.supplyAsync(() -> new CalcTask().RunWithResult(),joinPool);
        CompletableFuture<Void> cf2 = cf.thenApply((result) -> {
            System.out.println("thenApply -> " + System.currentTimeMillis());
            return "thenApplyResult:" + result;
        }).thenAccept((result) -> {
            //接收上一个任务的执行结果作为入参，但是没有返回值
            System.out.println("thenAccept -> " + System.currentTimeMillis());
        }).thenRun(()->{
            //无入参，也没有返回值
            System.out.println("thenRun -> "+System.currentTimeMillis());
            new CalcTask().Run();
        });
        System.out.println("主线程执行："+System.currentTimeMillis());
        System.out.println("cf 结果："+ cf.get());
        System.out.println("cf2 结果："+cf2.get());
        System.out.println("主线程结束:"+System.currentTimeMillis());
    }

}
